package com.crealth;

import java.util.List;

import org.apache.log4j.Logger;

import com.crealth.db.ClienteleDb;
import com.crealth.manager.MybatisManager;
import com.crealth.memcached.MemcachedMgr;
import com.crealth.model.ClienteleInfoModel;
import com.crealth.utils.PhoneNumDecode;
import com.crealth.utils.Utils;

public class EncryptPhoneNumMain {
	private static Logger logger = Logger.getLogger("running_log");
	private static Logger errorLog = Logger.getLogger("error_log");
	
	public static void main(String[] args) {
		try {
			String osName = Utils.getOSName();
			logger.info("OS: " + osName);			
			Config.initConfig();
			MybatisManager.initSessionFactory();			
		} catch (Exception e) {
			errorLog.error("init config error", e);
			System.exit(0);
		}		
		
		MemcachedMgr.initMemcached();
		int smallID = 0;
		while (true) {
			List<ClienteleInfoModel> clientelels = ClienteleDb.selectAllClienteleMobi(smallID);
			if (clientelels == null || clientelels.size() == 0) {
				break;
			}
			smallID = clientelels.get(clientelels.size()-1).getId();
			logger.info("smallID: " + smallID + "  clientelels size: " + clientelels.size());
			for (ClienteleInfoModel info : clientelels) {
				String phone2 = PhoneNumDecode.encryptPhone(info.getMobi());
				ClienteleDb.updateClienteMobi(info.getId(), phone2);
			}
		}
		logger.info("Clientele update finish-----");
		
		List<String> groupCallls = ClienteleDb.selectAllGroupCallMobiPhone();
		logger.info("groupCallls size: " + groupCallls.size());
		for (String mobi : groupCallls) {
			String setMobi = PhoneNumDecode.encryptPhone(mobi);
			ClienteleDb.updateGroupCallMobi(mobi, setMobi);
		}
		logger.info("groupCall update finish-----");
	}

}
